/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.contexts;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.part.Page;


/**
 *
 * ContextPage.java<br>
 * Created on: Jun 27, 2008<br>
 * <p>
 * A message page display a message in a pagebook view, and activates a context.
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 *
 * @see PageBookView
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class ContextPage extends Page {
    public static final String TEST_CONTEXT_ID = "org.eclipse.ui.tests.contexts.Page";
    private Composite pgComp;
    private Label msgLabel;
    private String message = ""; //$NON-NLS-1$

    /**
     * Creates a new page. The message is the empty string.
     */
    public ContextPage() {
        // do nothing
    }

    /*
     * (non-Javadoc) Method declared on IPage.
     */
    public void createControl(Composite parent) {
        // Message in default page of Outline should have margins
        pgComp = new Composite(parent, SWT.NULL);
        pgComp.setLayout(new FillLayout());

        msgLabel = new Label(pgComp, SWT.LEFT | SWT.TOP | SWT.WRAP);
        msgLabel.setText(message);
    }

    /*
     * (non-Javadoc) Method declared on IPage.
     */
    public Control getControl() {
        return pgComp;
    }

    /**
     * Sets focus to a part in the page.
     */
    public void setFocus() {
        // important to give focus to the composite rather than the label
        // as the composite will actually take focus (though hidden),
        // but setFocus on a Label is a no-op
        pgComp.setFocus();
    }

    /**
     * Sets the message to the given string.
     *
     * @param message
     *            the message text
     */
    public void setMessage(String msg) {
        this.message = msg;

        if (msgLabel != null)
            msgLabel.setText(msg);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.part.Page#init(org.eclipse.ui.part.IPageSite)
     */
    public void init(IPageSite pageSite) {
        super.init(pageSite);

        IContextService localService = (IContextService) getSite()
                                                             .getService(IContextService.class);
        localService.activateContext(TEST_CONTEXT_ID);
    }
}
